<template>
  <div class="app-container">
    <el-card class="content-edit" >
      <div slot="header">{{ pageType==='add'?'添加路线':'编辑路线' }}</div>
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="线路名称" maxlength="20">
          <el-input v-model="form['name']"/>
        </el-form-item>
        <el-form-item label="线路属性" maxlength="20">
          <el-radio-group v-model="form['hot']">
            <el-radio label="1">普通路线</el-radio>
            <el-radio label="2">热门路线</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="出发地">
          <el-select v-model="form['provinceCodeFromIdx']" placeholder="请选择省份" filterable
                     @change="e=>provinceSelectChange(e, 'From')">
            <el-option v-for="(item,index) in provinceList" :key="item.code" :label="item.name" :value="index"/>
          </el-select>
          <el-select v-model="form['cityCodeFromIdx']" placeholder="请选择市" filterable
                     @change="e=>citySelectChange(e, 'From')">
            <el-option v-for="(item,index) in FromCityList" :key="item.code" :label="item.name" :value="index"/>
          </el-select>
          <el-select v-if="FromAreaList.length!=0" v-model="form['areaCodeFrom']" placeholder="请选择区" filterable>
            <el-option v-for="item in FromAreaList" :key="item.code" :label="item.name" :value="item.code"/>
          </el-select>
          <el-input v-model="form['addressFrom']" :rows="2" type="textarea" placeholder="详细地址"/>
        </el-form-item>
        <el-form-item label="目的地">
          <el-select v-model="form['provinceCodeToIdx']" placeholder="请选择省份" filterable
                     @change="e=>provinceSelectChange(e, 'To')">
            <el-option v-for="(item,index) in provinceList" :key="item.code" :label="item.name" :value="index"/>
          </el-select>
          <el-select v-model="form['cityCodeToIdx']" placeholder="请选择市" filterable
                     @change="e=>citySelectChange(e, 'To')">
            <el-option v-for="(item,index) in ToCityList" :key="item.code" :label="item.name" :value="index"/>
          </el-select>
          <el-select v-if="ToAreaList.length!=0" v-model="form['areaCodeTo']" placeholder="请选择区" filterable>
            <el-option v-for="item in ToAreaList" :key="item.code" :label="item.name" :value="item.code"/>
          </el-select>
          <el-input v-model="form['addressTo']" :rows="2" type="textarea" placeholder="详细地址"/>
        </el-form-item>
        <el-form-item label="车型选择">
          <el-select v-model="form['carType']" placeholder="车辆类型">
            <el-option v-for="item in carTypeList" :key="item.id" :value="item['id']" :label="item['name']"/>
          </el-select>
        </el-form-item>
        <el-form-item label="车票价格" class="ticket">
          <el-input v-model="form['price']" type="number">
            <span slot="suffix" class="span">元</span>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button :loading="loading" type="primary" @click="onSubmit">保存</el-button>
          <el-button @click="$router.back()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
  export default {
    name: 'RouteDailyModify',
    data() {
      return {
        pageType: 'add',
        loading: false,
        // 司机列表
        driverList: [],
        // 车辆类型列表
        carTypeList: [],
        // 省市区列表
        provinceList: [],
        // 出发地市列表
        FromCityList: [],
        // 出发地区列表
        FromAreaList: [],
        // 出发地市列表
        ToCityList: [],
        // 出发地区列表
        ToAreaList: [],

        form: {
          // 路线名称
          name: '',
          // 路线属性
          hot: '1',
          // 车辆类型
          carType: '',
          // 出发地
          // 省
          provinceCodeFromIdx: '',
          provinceCodeFrom: '',
          // 市
          cityCodeFromIdx: '',
          cityCodeFrom: '',
          // 区
          areaCodeFrom: '',
          // 详细地址
          addressFrom: '',
          // 目的地
          // 省
          provinceCodeToIdx: '',
          provinceCodeTo: '',
          // 市
          cityCodeToIdx: '',
          cityCodeTo: '',
          // 区,
          areaCodeTo: '',
          // 详细地址
          addressTo: '',
          // 价格
          price: '',
          // 路线属性
          type: 0
        }
      }
    },
    mounted(){
      this.form['id'] = this.$router.currentRoute.query['id'] || '';
      if (this.$router.currentRoute.path.indexOf('add') > 0) {
        this.pageType = 'add'
      } else {
        this.pageType = 'edit'
      }
      this.loadCarTypeList();
    },
    methods: {
      // 加载车辆类型列表
      loadCarTypeList(){
        (async () => {
          const result = await this.$service.applicationGetAllCarType();
          if (result.code === 0) {
            this.carTypeList = result.data;
            this.loadProvinceList();
          }
        })();
      },
      // 加载省市区列表
      loadProvinceList(){
        (async () => {
          const result = await this.$service.applicationGetProvinceList();
          if (result.code === 0) {
            this.provinceList = result.data;
            if (this.pageType === 'edit') {
              this.loadData();
            }
          }
        })();
      },
      // 加载路线信息
      loadData(){
        (async () => {
          const result = await this.$service.applicationGetFixedRouteDetails({id: this.form['id']});
          if (result.code === 0) {
            const data = result.data || {};
            for (let i = 0; i < this.provinceList.length; i++) {
              if (this.provinceList[i].code === data['provinceCodeFrom']) {
                data['provinceCodeFromIdx'] = i;
                this.FromCityList = this.provinceList[i]['children'];
                for (let j = 0; j < this.provinceList[i]['children'].length; j++) {
                  if (this.provinceList[i]['children'][j].code === data['cityCodeFrom']) {
                    data['cityCodeFromIdx'] = j;
                    this.FromAreaList = this.provinceList[i]['children'][j]['children'] || [];
                  }
                }
              }
              if (this.provinceList[i].code === data['provinceCodeTo']) {
                data['provinceCodeToIdx'] = i;
                this.ToCityList = this.provinceList[i]['children'];
                for (let j = 0; j < this.provinceList[i]['children'].length; j++) {
                  if (this.provinceList[i]['children'][j].code === data['cityCodeTo']) {
                    data['cityCodeToIdx'] = j;
                    this.ToAreaList = this.provinceList[i]['children'][j]['children'] || [];
                  }
                }
              }
            }
            this.form = Object.assign(this.form, data);
          }
        })();
      },
      // 省份选择监听e:选择的index,params:出发地还是终点
      provinceSelectChange(e, params){
        // console.log(e, params);
        this[params + 'CityList'] = this.provinceList[e]['children'];
        this.form['provinceCode' + params] = this.provinceList[e]['code'];
        this.form['cityCode' + params + 'Idx'] = '';
        this.form['cityCode' + params] = '';
        this.form['areaCode' + params] = '';
      },
      // 市级选择监听e:选择的index,params:出发地还是终点
      citySelectChange(e, params){
        // console.log(e, params);
        this[params + 'AreaList'] = this[params + 'CityList'][e]['children'] || [];
        this.form['cityCode' + params] = this[params + 'CityList'][e]['code'];
        this.form['areaCode' + params] = '';
      },
      // 车票选择监听
      radioClick(params){
        this.form[params] = this.form[params] ? 0 : 1
      },
      // 提交
      onSubmit() {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.loading = true;
            (async () => {
              const params = Object.assign({}, this.form);
              let result = null;
              if (this.pageType === 'add') {
                result = await this.$service.applicationAddFixedRoute(params);
              } else {
                result = await this.$service.applicationModifyFixedRoute(params);
              }
              this.loading = false;
              if (result.code === 0) {
                this.$message.success('操作成功~');
                this.$router.back();
              } else {
                this.$message.error(result.msg);
              }
            })();
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }
</script>

<style lang="scss" type="text/scss" scoped>
  @import '../../../styles/index.scss';
  .ticket{
    .item{
      margin-bottom: 30px;
    }
    span{
      font-weight: 500;
      line-height: 38px;
    }
    .el-input{
      margin-top: 1px;
      width: 190px;
      .span{
        margin-right: 10px;
        line-height: 40px;
      }
    }
  }
</style>
